package com.zzk.lanouhn.myapplication1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.AllRelationArticlesAdapter;
import com.zzk.lanouhn.myapplication1.modles.AllRelationArticles;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/13.
 */
public class AllRelationActivity extends AppCompatActivity {

    private ImageButton btn_back;
    private XRecyclerView recyclerView;
    private String singleId;
    private int page = 0;
    private List<AllRelationArticles.DataBean> relationArticles=new ArrayList<>();
    private List<AllRelationArticles.DataBean> temp=new ArrayList<>();
    private AllRelationArticlesAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_relation_articles);
        initView();
        initData();
        initEvent();
        getData();
    }

    private void initView() {
        btn_back = (ImageButton) findViewById(R.id.back_act_all_relation);
        recyclerView = (XRecyclerView) findViewById(R.id.recycler_all_relation);
        LinearLayoutManager manager=new LinearLayoutManager(AllRelationActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
    }

    private void initData() {
        Intent intent = getIntent();
        singleId = intent.getStringExtra("singleId");
    }

    private void initEvent() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=0;
                getData();
            }

            @Override
            public void onLoadMore() {
                page+=1;
                getData();
            }
        });
    }

    private void getData() {
        String s = String.valueOf(page);
        String url = "http://open4.bantangapp.com/products/brandProduct/relationArticleTopicList?";
        String dataUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=&oauth_token=&track_deviceid=863121028526879&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2" +
                "&screensize=720&v=19&page=" + s +
                "&pagesize=10" +
                "&id=" + singleId;
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson=new Gson();
                    temp=gson.fromJson(result,AllRelationArticles.class).getData();
                    if (temp.size()>0) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 1:
                    if (page == 0) {
                        relationArticles.clear();
                        relationArticles.addAll(temp);
                        adapter = new AllRelationArticlesAdapter(AllRelationActivity.this, relationArticles);
                        recyclerView.setAdapter(adapter);
                        recyclerView.refreshComplete();
                        temp.clear();
                    } else {
                        relationArticles.addAll(temp);
                        adapter.notifyDataSetChanged();
                        recyclerView.loadMoreComplete();
                        temp.clear();
                    }
                    break;
              default:

                  break;
            }
        }
    };
}
